package fr.cephb.joperon.webapp.jpa;



import java.util.ArrayList;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * User
 * @author lindenb
 *
 */
@Entity
@Table(name="USERS",schema="OPERON")
@NamedQueries(value={
@NamedQuery(
	    name="user.byLogin",
	    query="SELECT p FROM User p WHERE p.login = :login"
	    ),
@NamedQuery(
	    name="user.anonymous",
	    query="SELECT p FROM User p WHERE p.login = \"anonymous\""
	    ),
@NamedQuery(
	    name="user.all",
	    query="SELECT p FROM User p ORDER BY p.login"
	    ),
@NamedQuery(
    name="user.byLoginPassword",
    query="SELECT p FROM User p WHERE p.login = :login and p.sha1Password = :sha1"
    )
})
public class User
	{
	public static final int ROOT_ID=1;
	public static final int ANONYMOUS_ID=2;
	private int id;
	private String login;
	private String sha1Password;
	private boolean administrator=false;
	private  List<Access> access= new ArrayList<Access>(1);
	
	public User()
		{
		
		}
	
	public User(String login)
		{
		this.login=login;
		if(this.login==null) throw new NullPointerException("User.login==null");
		}
	
	 @Id
     @Column(name="ID")
     @GeneratedValue(strategy=GenerationType.IDENTITY)
     public int getId() {
             return id;
     }

     @SuppressWarnings("unused")
	private void setId(int id) {
             this.id = id;
     }

     @Column(name="LOGIN",updatable=false,unique=true,length=20,nullable=false)
     public String getLogin() {
             return login;
     }
     
     public void setLogin(String login) {
             this.login = login;
     }
	
     
     @Column(name="SHA1PASSWORD",updatable=false,nullable=false,length=50)
     @Basic(fetch=FetchType.LAZY)
     public String getSha1Password() {
             return sha1Password;
             }
     
     public void setSha1Password(String sha1Password) {
             this.sha1Password = sha1Password;
             }
     
     
     @OneToMany(fetch=FetchType.LAZY,mappedBy="user",targetEntity=Access.class)
     public List<Access> getAccess()
             {
             return this.access;
             }
     
     public void setAccess(List<Access> access) {
             this.access = access;
             }
     
     
	@Override
	public int hashCode() {
		return getLogin().hashCode();
		}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		return getLogin().equals(User.class.cast(obj).getLogin());
		}
	
	@Override
		public String toString() {
			return "User{id:"+getId()+",login:"+getLogin()+"}";
			}
	
	
	@Transient
	public boolean isRoot()
		{
		return getId()==1 || isAdministrator();
		}
	
	public boolean isAnonymous()
		{
		return getId()==2;
		}
	
	
	public void setAdministrator(boolean administrator) {
		this.administrator = administrator;
		}
	
	@Column(name="is_admin")
	public boolean isAdministrator()
		{
		return administrator;
		}
	
	
	public boolean canReadProject(String pid)
		{
		if(isRoot()) return true;
		for(Access acc:getAccess())
			{
			if(acc.getProject().equals(pid)) return true;
			}
		return false;
		}
	
	public boolean canAdminProject(String pid)
		{
		if(isRoot()) return true;
		for(Access acc:getAccess())
			{
			if(acc.isAdministrator() &&
			   acc.getProject().equals(pid))
				{
				return true;
				}
			}
		return false;
		}
	
	
	public boolean canWriteProject(String pid)
		{
		if(isRoot() || canAdminProject(pid)) return true;
		for(Access acc:getAccess())
			{
			if(acc.isWriter() && acc.getProject().equals(pid)) return true;
			}
		return false;
		}
	}
